<template>
  <div class="app-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>角色列表</span>
          <el-button type="primary" @click="handleAdd">添加角色</el-button>
        </div>
      </template>

      <el-table :data="roleList" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="角色名称" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="菜单权限" width="300">
          <template #default="{ row }">
            <el-tag v-for="menu in row.menus" :key="menu.id" class="menu-tag">
              {{ menu.name }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button size="small" @click="handleEdit(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 角色表单对话框 -->
    <el-dialog
      :title="dialogType === 'create' ? '添加角色' : '编辑角色'"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="roleForm.description" type="textarea" />
        </el-form-item>
        <el-form-item label="菜单权限" prop="menuIds">
          <el-select v-model="roleForm.menuIds" multiple>
            <el-option
              v-for="menu in menuList"
              :key="menu.id"
              :label="menu.name"
              :value="menu.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { getRoles, createRole, updateRole, deleteRole } from '@/api/role'
import { getMenus } from '@/api/menu'
import { ElMessage, ElMessageBox } from 'element-plus'

export default {
  name: 'RoleManagement',
  setup() {
    const roleList = ref([])
    const menuList = ref([])
    const dialogVisible = ref(false)
    const dialogType = ref('create')
    const roleForm = ref({
      id: null,
      name: '',
      description: '',
      menuIds: []
    })

    const rules = {
      name: [
        { required: true, message: '请输入角色名称', trigger: 'blur' }
      ],
      description: [
        { required: true, message: '请输入角色描述', trigger: 'blur' }
      ]
    }

    const fetchRoles = async () => {
      try {
        const data = await getRoles()
        roleList.value = data
      } catch (error) {
        console.error('获取角色列表失败:', error)
      }
    }

    const fetchMenus = async () => {
      try {
        const data = await getMenus()
        menuList.value = data
      } catch (error) {
        console.error('获取菜单列表失败:', error)
      }
    }

    const handleAdd = () => {
      dialogType.value = 'create'
      dialogVisible.value = true
      roleForm.value = {
        id: null,
        name: '',
        description: '',
        menuIds: []
      }
    }

    const handleEdit = (row) => {
      dialogType.value = 'update'
      dialogVisible.value = true
      roleForm.value = {
        ...row,
        menuIds: row.menus.map(menu => menu.id)
      }
    }

    const handleDelete = async (row) => {
      try {
        await ElMessageBox.confirm('确认删除该角色吗？', '提示', {
          type: 'warning'
        })
        await deleteRole(row.id)
        ElMessage.success('删除成功')
        fetchRoles()
      } catch (error) {
        console.error('删除角色失败:', error)
      }
    }

    const submitForm = async () => {
      try {
        if (dialogType.value === 'create') {
          await createRole(roleForm.value)
        } else {
          await updateRole(roleForm.value.id, roleForm.value)
        }
        ElMessage.success(dialogType.value === 'create' ? '添加成功' : '更新成功')
        dialogVisible.value = false
        fetchRoles()
      } catch (error) {
        console.error('操作失败:', error)
      }
    }

    onMounted(() => {
      fetchRoles()
      fetchMenus()
    })

    return {
      roleList,
      menuList,
      dialogVisible,
      dialogType,
      roleForm,
      rules,
      handleAdd,
      handleEdit,
      handleDelete,
      submitForm
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.menu-tag {
  margin-right: 5px;
  margin-bottom: 5px;
}
</style> 